package xyz.codingli.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
 * @author mizzle rain
 * @date 2021-01-10 16:56
 */
public class ParamServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        System.out.println("get name = " + name);
        String str = paramsToString(req);
        resp.getWriter().print("get " + str);
    }

    private String paramsToString(HttpServletRequest req) {
        Enumeration<String> names = req.getParameterNames();
        StringBuffer sb = new StringBuffer();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = req.getParameter(name);
            sb.append(name).append(":").append(value).append(" ");
        }
        return sb.toString();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        System.out.println("post name = " + name);
        String str = paramsToString(req);
        resp.getWriter().print("post " + str);
    }
}
